package service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import util.JsonTools;

import db.DBConn;

public class SaleCountService {
	// 保存订单信息
	public static String saleCount() throws SQLException {

		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		Map<String, Integer[]> saleMap = new HashMap<String, Integer[]>();

		String sql = "select  * from shop_order,shop_seller where seller_id = order_sellerId ";
		ResultSet rs = stmt.executeQuery(sql);
		while (rs.next()) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			Date now = new Date();
			Date addTime = rs.getDate("order_addTime");

			long test = now.getTime() - addTime.getTime();

			long month = test / (30 * 24 * 60 * 60 * 1000L);


			// System.out.println((int)month);
			if ((int) month < 6) {
				String sellerName = rs.getString("seller_name");
				if (saleMap.containsKey(sellerName)) {
					saleMap.get(sellerName)[(int) month]++;
				} else {
					Integer[] temp = { 0, 0, 0, 0, 0, 0 };
					temp[(int) month]++;
					saleMap.put(sellerName, temp);
				}
			}
		}

		// System.out.println(JsonTools.createJsonString(saleMap));
		Set<String> keySet = saleMap.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			Integer[] a = saleMap.get(it.next());
			for (int i = 0; i < a.length; i++) {
				if (i % 6 == 0)
					System.out.println("=======");
				System.out.println(a[i]);
			}
		}

		stmt.close();
		conn.close();
		// 数据库操作失败，返回ERROR
		return JsonTools.createJsonString(saleMap);
	}

	public static void main(String[] args) throws SQLException {
		saleCount();
	}
}
